package com.blast.manager.delay.presenter

import com.blast.manager.delay.DelayActivity
import com.blast.manager.pop.DelaySortPop
import com.blast.manager.pop.DeleteTipPop
import com.blast.manager.uitl.SortUtil
import com.wxl.common.AppContext
import com.wxl.common.base.AbsActivity
import com.wxl.common.life.ViewModelQuick
import com.wxl.common.room.entity.LeiGuanEntity
import com.wxl.common.room.helper.DBHelper
import com.wxl.common.util.ObservableUtil
import com.wxl.common.util.Toll
import io.reactivex.functions.Action


/**
 * 单条数据的增删改，排序
 */
class DataHandlePresenter : AbsDelayPresenter() {

    override fun onDelayCreated(vararg args: Any) {

    }


    /**
     * 删除选中的条目
     */
    fun deleteSelectPosition(){
        ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).apply {
            if(getSelectPosition() >= 0 && getData().isNotEmpty()){
                DeleteTipPop.show(context, Action {
                    DBHelper.deleteLeiGuan(object : ObservableUtil.Companion.OnNextCallback<Boolean>(){
                        override fun onNext(t: Boolean) {
                            (context as DelayActivity).addLog("删除方案","点击清除方案${getSelectPosition()}")
                            notifyItemRemoved(getSelectPosition())
                        }
                    },getData()[getSelectPosition()])
                },"确定删除该发雷管信息？")
            } else {
                Toll.toll("没有雷管数据")
            }
        }
    }

    /**
     * 修改选择的条目
     */
    fun updateSelectPosition(){
        ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).run {
            if(getSelectPosition() >= 0 && !isEditor() && getData().isNotEmpty()){
                getData()[getSelectPosition()].editor = 2
                notifyItemDataChanged(getSelectPosition())
            } else {
                Toll.toll("没有雷管数据")
            }
        }
    }


    /**
     * 排序
     */
    fun sortData(){
        DelaySortPop.show(context,object : ObservableUtil.Companion.OnNextCallback<Int>(){
            override fun onNext(t: Int) {
                when (t) {
                    DelaySortPop.SORT_HOLE_UP_CODE -> {
                        SortUtil.sort(ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).getData())
                        ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).refreshData()
                    }
                    DelaySortPop.SORT_HOLE_DOWN_DODE -> {
                        SortUtil.reverseSort(ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).getData())
                        ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).refreshData()
                    }
                    DelaySortPop.SORT_DELAY_UP_CODE -> {
                        SortUtil.delaySort(ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).getData())
                        ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).refreshData()
                    }
                    DelaySortPop.SORT_DELAY_DOWN_CODE -> {
                        SortUtil.delayReverseSort(ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).getData())
                        ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).refreshData()
                    }
                    DelaySortPop.SORT_NUM_UP_CODE -> {
                        SortUtil.codeSort(ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).getData())
                        ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).refreshData()
                    }
                    DelaySortPop.SORT_NUM_DOWN_CODE -> {
                        SortUtil.codeReverseSort(ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).getData())
                        ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).refreshData()
                    }
                }
            }
        })
    }


    /**
     * 插入
     */
    fun insertItem(){
        val maxNum = AppContext.appContext.getLeiGuanMaxNum()
        if(ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).getData().size >= maxNum){
            Toll.toll("已达到最多组网数")
            return
        }

        if(ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).getData().isEmpty()){
            Toll.toll("没有雷管数据")
            return
        }

        ViewModelQuick.get(context as AbsActivity,ShowDelayDataPresenter::class.java).run {
            if((getSelectPosition() >= 0) && !isEditor()){
                (context as DelayActivity).addLog("插入方案","插入方案${getSelectPosition()}")
                val data = LeiGuanEntity()
                data.editor = 1
                data.delay = ""
                data.rowNumber = ""
                data.holeNumber = ""
                data.collectorNumber = ""
                data.UID = ""
                data.isSelect = true

                val index = if(getSelectPosition() >= 0) getSelectPosition() else 0
                getData()[getSelectPosition()].isSelect = false
                notifyCanScrollVerticallyChanged(getSelectPosition() != 0)
                getData().add(index,data)
                refreshData()
            }
        }
    }
}